﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework.Common
{
    public class CommonMethod
    {
        private CommonMethod()
        { }
        private static CommonMethod instance = new CommonMethod();

        public static CommonMethod GetInstance
        {
            get
            {
                if (instance == null)
                {
                    return new CommonMethod();
                }
                return instance;
            }
        }

        public string GenerateCode(string header)
        {
            string date = DateTime.Now.ToString("yyyyMMddHHmmss");

            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            int TimeId = (int)(DateTime.Now - startTime).TotalSeconds;
            string strTime = TimeId.ToString().Substring(TimeId.ToString().Length - 3);

            string code = header + date + strTime;
            return code;
        }

        public string GenerateRandomCode(int length)
        {
            if (length <= 0)
                length = 6;

            string date = DateTime.Now.ToString("yyyyMMddHHmmss");

            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            int TimeId = (int)(DateTime.Now - startTime).TotalSeconds;
            string strTime = TimeId.ToString().Substring(TimeId.ToString().Length - length);

            return strTime;
        }

        private static readonly String cnNumber = "零壹贰叁肆伍陆柒捌玖";
        private static readonly String cnUnit = "分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟";

        public String GetRMBString(String MoneyString)
        {
            String[] tmpString = MoneyString.Split('.');
            String intString = MoneyString;   // 默认为整数
            String decString = "";            // 保存小数部分字串
            String rmbCapital = "";            // 保存中文大写字串
            int k;
            int j;
            int n;

            if (tmpString.Length > 1)
            {
                intString = tmpString[0];             // 取整数部分
                decString = tmpString[1];             // 取小数部分
            }
            decString += "00";
            decString = decString.Substring(0, 2);   // 保留两位小数位
            intString += decString;

            try
            {
                k = intString.Length - 1;
                if (k > 0 && k < 18)
                {
                    for (int i = 0; i <= k; i++)
                    {
                        j = (int)intString[i] - 48;
                        // rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i];     // 供调试用的直接转换
                        n = i + 1 >= k ? (int)intString[k] - 48 : (int)intString[i + 1] - 48; // 等效于 if( ){ }else{ }
                        if (j == 0)
                        {
                            if (k - i == 2 || k - i == 6 || k - i == 10 || k - i == 14)
                            {
                                rmbCapital += cnUnit[k - i];
                            }
                            else
                            {
                                if (n != 0)
                                {
                                    rmbCapital += cnNumber[j];
                                }
                            }
                        }
                        else
                        {
                            rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k - i];
                        }
                    }

                    rmbCapital = rmbCapital.Replace("兆亿万", "兆");
                    rmbCapital = rmbCapital.Replace("兆亿", "兆");
                    rmbCapital = rmbCapital.Replace("亿万", "亿");
                    rmbCapital = rmbCapital.TrimStart('元');
                    rmbCapital = rmbCapital.TrimStart('零');

                    return rmbCapital;
                }
                else
                {
                    return "";   // 超出转换范围时，返回零长字串
                }
            }
            catch
            {
                return "";   // 含有非数值字符时，返回零长字串
            }
        }
    }
}
